﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ITAP.ShprossesBuilder.Manipulation.PopupMenu
{
    public class ModelPopupMenu
    {
        private ContextMenuStrip menuStrip;
        private Dictionary<string, ICommand> popupItems;
        private ModelManipulator manipulator;

        public ModelPopupMenu(ModelManipulator manipulator)
        {
            popupItems = new Dictionary<string, ICommand>();
            menuStrip = new ContextMenuStrip();
            this.manipulator = manipulator;
        }

        public void AddSeparator() {
            menuStrip.Items.Add(new ToolStripSeparator());
        }

        public void AddPopupItem(string name, Image thumbnail, ICommand command)
        {
            popupItems.Add(name, command);
            EventHandler handler = new EventHandler(OnItemClick);

            
            ToolStripItem item = new ToolStripMenuItem(name, thumbnail, handler);
            item.Tag = command;
            menuStrip.Items.Add(item);
        }

        public void OnItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem button = sender as ToolStripMenuItem;
            ICommand command = button.Tag as ICommand;
            manipulator.StartExecute(command);
            manipulator.Execute(command);
            manipulator.EndExecute(command);
        }

        public void Show(Point location)
        {
            menuStrip.Show(location);
        }
    }
}
